package com.xckj.cycling.utils;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.Nullable;

import java.util.Stack;

/**
 * app栈管理
 */
public class AppManager {

    private static AppManager mAppManager;
    private static final Stack<Activity> mActivityStack = new Stack<>();

    public static AppManager getInstance() {
        if (mAppManager == null) {
            mAppManager = new AppManager();
        }
        return mAppManager;
    }

    /**
     * 添加Activity到堆
     */
    public void addActivity(Activity activity) {
        mActivityStack.add(activity);
    }

    /**
     * 获取栈顶Activity
     */
    public Activity getTopActivity() {
        if (mActivityStack.empty()) {
            return null;
        }
        return mActivityStack.lastElement();
    }

    public boolean containActivity(Class<?> cls) {
        try {
            for (Activity activity : mActivityStack) {
                if (activity.getClass().equals(cls)) {
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    public boolean containActivity(Activity activity) {
        try {
            return mActivityStack != null && mActivityStack.contains(activity);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Nullable
    public Activity getActivity(Class<?> cls) {
        try {
            if (mActivityStack != null) {
                for (Activity activity : mActivityStack) {
                    if (activity.getClass().equals(cls)) {
                        return activity;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }



    /**
     * 结束指定的Activity
     */
    public void killActivity(Activity activity) {
        if (activity != null && mActivityStack != null) {
            boolean res = mActivityStack.remove(activity);
            String actName = activity.getPackageName();
//            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void killActivity(Class<?> cls) {
        try {
            if (mActivityStack != null) {
                for (Activity activity : mActivityStack) {
                    if (activity.getClass().equals(cls)) {
                        killActivity(activity);
                        activity.finish();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 结束所有Activity
     */
    public void killAllActivity() {
        if (mActivityStack != null) {
            for (int i = 0, size = mActivityStack.size(); i < size; i++) {
                if (null != mActivityStack.get(i)) {
                    mActivityStack.get(i).finish();
                }
            }
            mActivityStack.clear();
        }
    }

    /**
     * 退出应用程序
     */
    public void AppExit(Context context) {
        try {
            killAllActivity();
            System.exit(0);
        } catch (Exception e) {
        }
    }


    public Stack<Activity> getActivityStack() {
        return mActivityStack;
    }
}
